package wtc.stu.dao;

import wtc.stu.entity.StuEntity;
import wtc.stu.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StuDaoMajor {
    private DBConnection dbc;
    private Connection conn = null;
    private ResultSet rs = null;
    private PreparedStatement pre = null;
    public StuDaoMajor()
    {
        dbc = new DBConnection();

    }
    // 全查询专业信息
    public List<StuEntity> getAll()
    {
        List<StuEntity> ar = new ArrayList<StuEntity>();
        try
        {
            conn = dbc.getConnection();
            String sql = "select * from majorinfo";
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next())
            {
                StuEntity major = new StuEntity();
                major.setZid(rs.getInt("zid"));
                major.setXid(rs.getInt("xid"));
                major.setZname(rs.getString("zname"));
                major.setZremark(rs.getString("zremark"));
                ar.add(major);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return ar;
    }
    // 查询一个专业表对象
    public StuEntity getOneById(int zid)
    {
        StuEntity major = new StuEntity();

        try
        {
            conn = dbc.getConnection();
            String sql = "select * from majorinfo where zid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, zid);
            rs = pre.executeQuery();
            while (rs.next())
            {
                major.setZid(rs.getInt("cid"));
                major.setXid(rs.getInt("xid"));
                major.setZname(rs.getString("zname"));
                major.setZremark(rs.getString("zremark"));

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return major;
    }
    // 增加专业信息表对象
    public void addStu(StuEntity major)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "insert into majorinfo(xid, zname, zremark) values(?,?,?)";
            pre = conn.prepareStatement(sql);
            pre.setInt(1,major.getXid());
            pre.setString(2,major.getZname());
            pre.setString(3, major.getZremark());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
    // 修改专业信息表对象
    public void updateStu(StuEntity major)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "update  majorinfo  set xid=?,zname=?, zremark=? where zid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, major.getXid());
            pre.setString(2, major.getZname());
            pre.setString(3, major.getZremark());
            pre.setInt(4, major.getZid());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
}
